package myJs.api

import japgolly.scalajs.react.extra.Ajax
import myJs.myPkg.MyFetch
import upickle.default._
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._
import shared.pojo.Browse._

/** Created by yz on 30/7/2023
  */
object Browse {

  val baseUrl = s"${Api.apiUrl}/browse"

  def downloadMummerFile(id: String, fileName: String) = s"${baseUrl}/downloadMummerFile?fileName=${id}/${fileName}"
  def getMummerImage(id: String, fileName: String) = s"${baseUrl}/getMummerImage?fileName=${id}/${fileName}"

  def allHost = {
    val url = s"${baseUrl}/allHost"
    MyFetch
      .getRnStr(
        url = s"${url}",
        headers = Map("Content-Type" -> "application/json")
      )
      .map { str =>
        read[List[String]](str)
      }
  }

  def getBioSample(id: String) = {
    val url = s"${baseUrl}/getBioSampleById?id=${id}"
    Ajax("GET", s"${url}").setRequestContentTypeJson.send.asAsyncCallback
      .map { xhr =>
        read[BioSampleInfo](xhr.responseText)
      }
  }

  def getAssembly(id: String) = {
    val url = s"${baseUrl}/getAssemblyById?id=${id}"
    Ajax("GET", s"${url}").setRequestContentTypeJson.send.asAsyncCallback
      .map { xhr =>
        read[AssemblyInfo](xhr.responseText)
      }
  }

  def getMummerFileData(request: GetFileData.Request) = {
    val url = s"${baseUrl}/getMummerFileData?id=${request.id}&fileName=${request.fileName}"
    Ajax("GET", s"${url}").setRequestContentTypeJson.send.asAsyncCallback
      .map { xhr =>
        read[GetFileData.Response](xhr.responseText)
      }
  }

}
